package com.bz.redis.service.impl;

import com.bz.pojo.TbUser;
import com.bz.redis.service.SSOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SSOServiceImpl implements SSOService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    private String user_session_redis_key = "user:session:redis:key";

    /**
     * 将用于缓存到Redis中
     * @param tbUser
     * @param userToken
     */
    @Override
    public void insertUser(TbUser tbUser, String userToken) {
        redisTemplate.opsForValue().set(user_session_redis_key+":"+userToken,tbUser,1, TimeUnit.DAYS);
    }

    /**
     * 用户退出登录
     * @param userToken
     */
    @Override
    public void logout(String userToken) {
        redisTemplate.delete(user_session_redis_key + ":" + userToken);
    }

    /**
     * 根据用户token判断用户在redis中是否失效
     * @param userToken
     * @return
     */
    @Override
    public TbUser checkUserToken(String userToken) {
        return (TbUser) redisTemplate.opsForValue().get(user_session_redis_key+":"+userToken);
    }
}
